package com.ubeecube.mylocker.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.Gallery;

import com.ubeecube.mylocker.R;

public class LockScreen extends Activity implements OnClickListener{
    private Button confirmBtn;
	private Gallery firstGallery;
	private Gallery secondGallery;
	private Gallery thirdGallery;
	private Gallery forthGallery;
	private NumberAdapter numberAdapter;
	private Animation mAnimLeftRightShow;
	private Animation mAnimLeftRightHide;
	private Animation mAnimRightLeftShow;
	private Animation mAnimRightLeftHide;
	private Animation mAnimBottomTopShow;
	private Animation mAnimBottomTopHide;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        confirmBtn = (Button) findViewById(R.id.confirm_btn);
        confirmBtn.setOnClickListener(this);
        
        firstGallery = (Gallery) findViewById(R.id.first_gallery);
        secondGallery = (Gallery) findViewById(R.id.second_gallery);
        thirdGallery = (Gallery) findViewById(R.id.third_gallery);
        forthGallery = (Gallery) findViewById(R.id.forth_gallery);
        
        numberAdapter = new NumberAdapter(this);
        
        firstGallery.setAdapter(numberAdapter);
        secondGallery.setAdapter(numberAdapter);
        thirdGallery.setAdapter(numberAdapter);
        forthGallery.setAdapter(numberAdapter);
        
        //To make the view go to the middle of our 'endless' array
        firstGallery.setSelection(NumberAdapter.MAX_GALLERY_VALUE / 2);
        secondGallery.setSelection(NumberAdapter.MAX_GALLERY_VALUE / 2);
        thirdGallery.setSelection(NumberAdapter.MAX_GALLERY_VALUE / 2);
        forthGallery.setSelection(NumberAdapter.MAX_GALLERY_VALUE / 2);
        
        mAnimLeftRightShow = AnimationUtils.loadAnimation(this, R.anim.left_to_right_show);
        mAnimLeftRightHide = AnimationUtils.loadAnimation(this, R.anim.left_to_right_hide);
        
        mAnimRightLeftShow = AnimationUtils.loadAnimation(this, R.anim.right_to_left_show);
        mAnimRightLeftHide = AnimationUtils.loadAnimation(this, R.anim.right_to_left_hide);
        
        mAnimBottomTopShow = AnimationUtils.loadAnimation(this, R.anim.bottom_to_top_show);
        mAnimBottomTopHide = AnimationUtils.loadAnimation(this, R.anim.bottom_to_top_hide);
        
        showLeftRightAnimation();
        
        showRightLeftAnimation();
        
        showBottomTopAnimation();
        
    }
    
    /**
     * Slide the search layout down
     */
    public void showLeftRightAnimation() {
        //Set the slide animation
    	firstGallery.setAnimation(mAnimLeftRightShow);
        //Start the slide animation
    	firstGallery.startAnimation(mAnimLeftRightShow);
        //Set the visibility of the search layout
    	firstGallery.setVisibility(View.VISIBLE);
    	
    	//Set the slide animation
    	thirdGallery.setAnimation(mAnimLeftRightShow);
        //Start the slide animation
    	thirdGallery.startAnimation(mAnimLeftRightShow);
        //Set the visibility of the search layout
    	thirdGallery.setVisibility(View.VISIBLE);
    	
    }
    
    /**
     * Slide the search layout up
     */
    public void hideLeftRightAnimation() {
        //Set the slide animation
    	firstGallery.setAnimation(mAnimLeftRightHide);
        //Start the slide animation
    	firstGallery.startAnimation(mAnimLeftRightHide);
        //Set the visibility of the search layout
    	firstGallery.setVisibility(View.INVISIBLE);
    	
    	//Set the slide animation
    	thirdGallery.setAnimation(mAnimLeftRightHide);
        //Start the slide animation
    	thirdGallery.startAnimation(mAnimLeftRightHide);
        //Set the visibility of the search layout
    	thirdGallery.setVisibility(View.INVISIBLE);
    }
    
    /**
     * Slide the search layout down
     */
    public void showRightLeftAnimation() {
        //Set the slide animation
    	secondGallery.setAnimation(mAnimRightLeftShow);
        //Start the slide animation
    	secondGallery.startAnimation(mAnimRightLeftShow);
        //Set the visibility of the search layout
    	secondGallery.setVisibility(View.VISIBLE);
    	
    	//Set the slide animation
    	forthGallery.setAnimation(mAnimRightLeftShow);
        //Start the slide animation
    	forthGallery.startAnimation(mAnimRightLeftShow);
        //Set the visibility of the search layout
    	forthGallery.setVisibility(View.VISIBLE);
    	
    }
    
    /**
     * Slide the search layout up
     */
    public void hideRightLeftAnimation() {
        //Set the slide animation
    	secondGallery.setAnimation(mAnimRightLeftHide);
        //Start the slide animation
    	secondGallery.startAnimation(mAnimRightLeftHide);
        //Set the visibility of the search layout
    	secondGallery.setVisibility(View.INVISIBLE);
    	
    	//Set the slide animation
    	forthGallery.setAnimation(mAnimRightLeftHide);
        //Start the slide animation
    	forthGallery.startAnimation(mAnimRightLeftHide);
        //Set the visibility of the search layout
    	forthGallery.setVisibility(View.INVISIBLE);
    }
    
    /**
     * Slide the search layout down
     */
    public void showBottomTopAnimation() {
        //Set the slide animation
    	confirmBtn.setAnimation(mAnimBottomTopShow);
        //Start the slide animation
    	confirmBtn.startAnimation(mAnimBottomTopShow);
        //Set the visibility of the search layout
    	confirmBtn.setVisibility(View.VISIBLE);
    }
    
    /**
     * Slide the search layout up
     */
    public void hideBottomTopAnimation() {
        //Set the slide animation
    	confirmBtn.setAnimation(mAnimBottomTopHide);
        //Start the slide animation
    	confirmBtn.startAnimation(mAnimBottomTopHide);
        //Set the visibility of the search layout
    	confirmBtn.setVisibility(View.INVISIBLE);
    }

	@Override
	public void onClick(View v) {
		if (v == confirmBtn) {
			
			Log.e("getView", " firstGallery : " + numberAdapter.getPosition(firstGallery.getSelectedItemPosition()));
			Log.e("getView", " secondGallery : " + numberAdapter.getPosition(secondGallery.getSelectedItemPosition()));
			Log.e("getView", " thirdGallery : " + numberAdapter.getPosition(thirdGallery.getSelectedItemPosition()));
			Log.e("getView", " forthGallery : " + numberAdapter.getPosition(forthGallery.getSelectedItemPosition()));
			
			closeActivity();
		}
		
	}
	
	/**
     * Closes the drop down activity
     */
    private void closeActivity() {
        hideBottomTopAnimation();
        
        hideRightLeftAnimation();
        
        hideLeftRightAnimation();
        
        new Handler().postDelayed(new Runnable() {
            /*
             * (non-Javadoc)
             * @see java.lang.Runnable#run()
             */
            @Override
            public void run() {
            	Intent intent = new Intent(LockScreen.this, LockerPhotos.class);
    			startActivity(intent);
                LockScreen.this.finish();
            }
        }, 600);
    }
}